package models

import (
	"gorm.io/gorm"
	"order_srv/basic/config"
	"time"
)

// 订单疑义表
type Doubt struct {
	Id             uint64         `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	DoubtOn        string         `gorm:"column:doubt_on;type:varchar(255);comment:订单编号;not null;" json:"doubt_on"`                                       // 订单编号
	OrderId        uint64         `gorm:"column:order_id;type:bigint UNSIGNED;comment:订单id;not null;" json:"order_id"`                                    // 订单id
	UserId         uint64         `gorm:"column:user_id;type:bigint UNSIGNED;comment:用户id;not null;" json:"user_id"`                                      // 用户id
	PreviousAmount float64        `gorm:"column:previous_amount;type:decimal(10, 2);comment:调帐前账单;not null;" json:"previous_amount"`                      // 调帐前账单
	LaterAmount    float64        `gorm:"column:later_amount;type:decimal(10, 2);comment:调帐后账单;not null;" json:"later_amount"`                            // 调帐后账单
	StartTime      time.Time      `gorm:"column:start_time;type:datetime(3);comment:开始时间;not null;default:CURRENT_TIMESTAMP(3);" json:"start_time"`       // 开始时间
	EndTime        time.Time      `gorm:"column:end_time;type:datetime(3);comment:结束时间;not null;default:CURRENT_TIMESTAMP(3);" json:"end_time"`           // 结束时间
	AdjustmentType uint8          `gorm:"column:adjustment_type;type:tinyint UNSIGNED;comment:调帐类型 1-充值 2-扣款;not null;default:2;" json:"adjustment_type"` // 调帐类型 1-充值 2-扣款
	Reason         string         `gorm:"column:reason;type:varchar(255);comment:调帐原因;not null;" json:"reason"`                                           // 调帐原因
	Status         uint8          `gorm:"column:status;type:tinyint UNSIGNED;comment:状态 1-待审核 2-已通过 3-未通过;not null;default:1;" json:"status"`             // 状态 1-待审核 2-已通过 3-未通过
	CreatedAt      time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt      time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt      gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (d *Doubt) TableName() string {
	return "doubt"
}

// 订单疑义添加
func (d *Doubt) DoubtAdd() error {
	return config.DB.Create(&d).Error
}

// 订单疑义删除
func (d *Doubt) DoubtDel() error {
	return config.DB.Where("id = ?", d.Id).Delete(&d).Error
}

// 订单疑义修改
func (d *Doubt) DoubtUpdate() error {
	return config.DB.Model(&d).Where("id = ?", d.Id).Updates(map[string]interface{}{
		"later_amount":    d.LaterAmount,
		"adjustment_type": d.AdjustmentType,
	}).Error
}

// 修改订单状态
func (d *Doubt) DoubtUpdateStatus() error {
	return config.DB.Model(&d).Where("id = ?", d.Id).Update("status", d.Status).Error
}

// 订单疑义展示
func (d *Doubt) DoubtList(page, pageSize int) (DoubtList []*Doubt, err error) {
	query := config.DB.Model(&Doubt{}).Scopes(Paginate(page, pageSize))
	if d.Status != 0 {
		query = query.Where("status = ?", d.Status)
	}
	err = query.Find(&DoubtList).Error
	return
}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}

// 订单详情
func (d *Doubt) DoubtDetail(Id int) error {
	return config.DB.Model(&d).Where("id = ?", Id).Limit(1).Find(&d).Error
}

// 订单id查询
func (d *Doubt) GetDoubtOrderIdInfo() error {
	return config.DB.Where("order_id = ?", d.OrderId).Limit(1).Find(&d).Error
}
